package cn.qn.service.impl.news;

import cn.qn.base.PageVO;
import cn.qn.dto.news.VideoDTO;
import cn.qn.dto.news.VideoQueryDTO;
import cn.qn.mapper.news.VideoMapper;
import cn.qn.moudle.news.Video;
import cn.qn.repository.image.ImageRepository;
import cn.qn.repository.news.VideoRepository;
import cn.qn.service.news.VideoService;
import cn.qn.util.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jeet
 * @description
 * @date 2019/5/31 17:57
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = VideoService.class)
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoRepository videoRepository;
    @Resource
    private VideoMapper videoMapper;
    @Autowired
    private ImageRepository imageRepository;

    @Override
    public Video insert(VideoDTO dto) {
        Video video = new Video();
        BeanUtils.copyProperties(dto,video);
        return videoRepository.save(video);
    }

    @Override
    public Video update(VideoDTO dto) {
        Video video = videoRepository.findById(dto.getId()).get();
        video.setId(dto.getId());
        video.setCover(dto.getCover());
        video.setBanner(dto.getBanner());
        video.setUpdateBy(dto.getUpdateBy());
        video.setName(dto.getName());
        video.setRemark(dto.getRemark());
        video.setStatus(dto.getStatus());
        video.setType(dto.getType());
        video.setUrl(dto.getUrl());
        videoRepository.save(video);
        return video;
    }

    @Override
    public PageVO<VideoDTO> page(VideoQueryDTO baseQuery) {
        PageBounds pb = baseQuery.getPageBounds();
        List<VideoDTO> list = videoMapper.page(pb,baseQuery);
        Integer pageCount = videoMapper.pageCount(baseQuery);
        return PageVO.convert(pb.getPage(),pb.getLimit(),list,pageCount);
    }

    @Override
    public VideoDTO findById(Integer id) {
        Video album = videoRepository.findById(id).get();
        VideoDTO dto = new VideoDTO();
        BeanUtils.copyProperties(album,dto);

        dto.setCreateTime(DateUtils.parseDateToStr(album.getCreateTime(),DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        dto.setCoverPath(imageRepository.findImageById(album.getCover()).getPath());
        dto.setBannerPath(imageRepository.findImageById(album.getBanner()).getPath());
        return dto;
    }
}
